﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 僵尸状态
public enum ZombieState
{
    None = -1,
    Walk = 1,
    DieWalk = 2,   // 死亡后继续行走
    Die = 3,
    Attack = 10,
    DieAttack = 11,   // 死亡后继续攻击
}

// 场景内僵尸基类
public class ZombieInScene : CreatureInScene
{
    ZombieDynamicData _dData;

    // 僵尸进攻线路
    AttackLine _attackLine;

    Rigidbody2D _rig;

    ZombieState _state = ZombieState.Walk;

    internal void Init(AttackLine aline)
    {
        base.InitData(new CreatureTableData(), new ZombieDynamicData());

        _dData = _dynamicData as ZombieDynamicData;

        _attackLine = aline;

        var obj = ResMgr.instance.InstanceGameObject("FightScene/Zombie/Zombie1/Zombie1");
        _modelRoot = obj.transform;
        _modelRoot.position = aline.StartPoint;

        initModel(_modelRoot);

        aline.AllZombie.Add(this);

        _rig = obj.AddComponent<Rigidbody2D>();
        _rig.useAutoMass = false;
        _rig.gravityScale = 0;
        _rig.isKinematic = true;

        _rig.velocity = Vector2.left * _dData.MoveSpeed;
    }

    public override bool CanBeAttackBy(CreatureInScene attacker)
    {
        if (!base.CanBeAttackBy(attacker)) { return false; }

        // 僵尸只能被植物攻击
        var plant = attacker as PlantInScene;
        if (plant == null) { return false; }

        return true;
    }

    // 改变状态
    private void changeState(ZombieState newState)
    {
        _state = newState;
        playAni((int)_state);
    }

    protected override void onDie()
    {
        // 死亡动画等等
        var dieLine = new TimeLine();
        dieLine.AddEvent(0, -1, onDieContinue);
        dieLine.AddEvent(0, 1.71f, onCutHead);

        dieLine.AddEvent(1.2f, -1, onDropDown);
        dieLine.AddEvent(3, -1, onRelease);
        dieLine.AddEvent(3, dieLine, onDieEnd);

        dieLine.Start();
    }

    // 死亡时间线结束
    private void onDieEnd(object obj)
    {
        (obj as TimeLine).Reset(); // 施放时间线
    }

    // 释放资源
    private void onRelease(int obj)
    {
        // 解除引用
        _attackLine.AllZombie.Remove(this);

        ResMgr.instance.ReleaseObject(_modelRoot.gameObject);
    }

    // 倒地
    private void onDropDown(int obj)
    {
        changeState(ZombieState.Die);
    }
    /// <summary>
    /// 掉头(实际上是生成头)
    /// </summary>
    /// <param name="delayDestroy">延迟销毁时间</param>
    private void onCutHead(object delayDestroy)
    {
        var headObj = ResMgr.instance.InstanceGameObject(@"FightScene\Zombie\Zombie1/Zombie1Head", _modelRoot.position);

        GameObject.Destroy(headObj, Convert.ToSingle(delayDestroy));
    }

    // 延迟死亡
    private void onDieContinue(int obj)
    {
        // 换成死亡状态机
        _ani.runtimeAnimatorController = ResMgr.instance.LoadTemplate(@"ZombieAni\Zombie1/ZombieDie") as RuntimeAnimatorController;

        // 根据延迟状态，确定是否要停止移动
        changeState(ZombieState.DieWalk);
        _rig.velocity = Vector2.zero;
    }
}

